{
	UCE - cross-platform game engine written in delphi.
	Copyright (c) 2009, Shutko Vitaly

	This file is part of UCE.

	UCE is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	UCE is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
}

unit uGam;

interface

uses
  uTyp;

type
  TGamSta = class(TList)
    procedure OnInit; virtual;
    procedure OnFrame; virtual;
    procedure OnStep; virtual;
    procedure OnFree; virtual;

    procedure OnEnter; virtual;
    procedure OnLeave; virtual;
  end;

  TGam = class(TGamSta)
    curSta: TGamSta;

    procedure Init;
    procedure Draw;
    procedure Update;
    procedure Enter(sta: TGamSta); overload;
    procedure Enter(name: String); overload;

    function Add(sta: TGamSta): TGamSta;
    function Find(name: String): TGamSta;
    procedure Rem(sta: TGamSta);

    constructor Create;
    destructor Destroy; override;
  end;

implementation

uses
  uVar;

//////////////////////////////////////////////////////////////////////////////
///  TGamSta
//////////////////////////////////////////////////////////////////////////////

procedure TGamSta.OnInit; begin end;
procedure TGamSta.OnFrame; begin end;
procedure TGamSta.OnStep; begin end;
procedure TGamSta.OnFree; begin end;
procedure TGamSta.OnEnter; begin end;
procedure TGamSta.OnLeave; begin end;

//////////////////////////////////////////////////////////////////////////////
///  TGam
//////////////////////////////////////////////////////////////////////////////

constructor TGam.Create;
begin
  curSta := nil;
end;

destructor TGam.Destroy;
var
  i: Integer;
begin
  for i := 0 to size - 1 do
    if items[i] <> nil then
    begin
      TGamSta(items[i]).OnFree;
      items[i].Free;
    end;
end;

procedure TGam.Init;
var
  i: Integer;
begin
  for i := 0 to size - 1 do
    if items[i] <> nil then TGamSta(items[i]).OnInit;
end;

procedure TGam.Draw;
var
  i: Integer;
begin
  for i := 0 to size - 1 do
    if items[i] <> nil then TGamSta(items[i]).OnFrame;
end;

procedure TGam.Update;
var
  i: Integer;
begin
  for i := 0 to size - 1 do
    if items[i] <> nil then TGamSta(items[i]).OnStep;
end;

procedure TGam.Enter(sta: TGamSta);
begin
  if curSta <> nil then
  begin
    curSta.OnLeave;
    curSta := nil;
  end;

  if sta <> nil then
  begin
    curSta := sta;
    curSta.OnEnter;
  end;
end;

procedure TGam.Enter(name: String);
begin
  Enter(Find(name));
end;

function TGam.Add(sta: TGamSta): TGamSta;
begin
  Result := TGamSta(inherited Add(sta));
end;

function TGam.Find(name: String): TGamSta;
begin
  Result := TGamSta(inherited Find(name));
end;

procedure TGam.Rem(sta: TGamSta);
begin
  if sta <> nil then
  begin
    inherited Rem(sta);
    sta.Free;
  end;
end;

initialization
  gam := TGam.Create;

finalization
  gam.Free;
end.
